package com.stockz.indicator.smartVolume;

import java.util.List;

import com.stockz.core.model.Bar;
import com.stockz.core.util.BarUtil.BarValueType;
import com.stockz.core.util.ListUtil;
import com.stockz.indicator.Indicator;
import com.stockz.ui.swing.charting.display.indicator.SimpleMovingAverage;
import com.stockz.ui.swing.charting.display.indicator.Stochastics;

public class SmartVolumeIndicator implements Indicator<SmartVolumeIndicatorConfiguration> {

	private final SmartVolumeIndicatorConfiguration configuration = 
			new SmartVolumeIndicatorConfiguration();
	
	@Override
	public SmartVolumeIndicatorConfiguration getIndicatorConfiguration() {
		return configuration;
	}

	@Override
	public BarValueType getBarValueType() {
		return null;
	}

	@Override
	public void setBarValueType(BarValueType barValueType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double[] getValues(List<Bar> barList) {
		if(barList == null 
				|| barList.size() < configuration.getStochasticsSpan()
				|| barList.size() < configuration.getVolumeSmaSpan())
		return new double[0];
		double[] closes = ListUtil.convert(barList, BarValueType.CLOSE);
		double[] volumeSmaValues = SimpleMovingAverage.values(closes, configuration.getVolumeSmaSpan());
		double[] stochasticsValues = Stochastics.values(closes, configuration.getStochasticsSpan());
		double[] values = new double[barList.size()];
		
		int higher = configuration.getVolumeSmaSpan();
		if(configuration.getStochasticsSpan() >
				configuration.getVolumeSmaSpan()){
			higher = configuration.getStochasticsSpan();
		}
		
		double value = 0;
		for(int i = barList.size() - higher - 1; i >= 0; i--){
			if(volumeSmaValues[i]*configuration.getVolumeMinimumMultiple()
					< barList.get(i).getVolume()){
				if(stochasticsValues[i] < configuration.getStochasticsUpperLimit()){
					value += (barList.get(i).getVolume()-volumeSmaValues[i]);
				}
				if(stochasticsValues[i] > configuration.getStochasticsLowerLimit()){
					value -= (barList.get(i).getVolume()-volumeSmaValues[i]);
				}
				if(i < barList.size() - configuration.getSpan()){
					value -= values[i+configuration.getSpan()];
				}
				values[i] = value;
			}
		}
		
		
		return values;
	}

	@Override
	public double getValue(List<Bar> barList) {
		// TODO Auto-generated method stub
		return 0;
	}

}
